package com.osgc.rf.unit.base;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.inca.np.auth.Userruninfo;
import com.osgc.rf.np.url.dao.DataConnect;
import com.osgc.rf.np.url.dao.Navigation;
import com.osgc.rf.np.url.dao.Page;
import com.osgc.rf.np.url.dao.User;
import com.osgc.rf.np.url.util.Base;
import com.osgc.rf.unit.dao.ResultInfo;
import com.osgc.rf.unit.dao.TaskInfo;

public abstract class NpWebModel extends Base {

	protected Navigation navigation = null;
	protected User user;
	protected Userruninfo userinfo;

	public void setInfoFileVm(String vm) {
		sessionHelper.putContext("infofilevm", vm);
	}

	public void index() {
		navigation.setId("");
		sessionHelper.remove();
		setInfoFileVm("scan");
	}

	 public String getCountSql(String groupid){return "";};

	abstract protected String getTemp();

	@Override
	public void beforeInvoke() throws Exception {
		Object o = sessionHelper.getContext("navigation");
		if (o instanceof Navigation) {
			navigation = (Navigation) o;
		}

		user = (User) sessionHelper.getContext("user");

		if (user != null) {
			userinfo = new Userruninfo();
			userinfo.setUserid(user.getUserid());
			userinfo.setUsername(user.getUsername());

			if (navigation == null) {
				navigation = new Navigation();
				sessionHelper.putContext("navigation", navigation);
			}
			navigation.setMenu(user.getOp());
		}
		setRedirect(getTemp());
	}

	@Override
	public void afterInvoke() throws Exception {

	}

	protected List<TaskInfo> getTask(Connection con, String barcode)
			throws Exception {
		return null;
	}

	public void query() {
		String barcode = params.get("barcode").trim();
		Connection con = null;
		try {
			con = DataConnect.getConnect();
			List<TaskInfo> infos = getTask(con, barcode);
			setInfos(infos);
			con.commit();
		} catch (Exception e) {
			logger.error(e.getMessage());
			DataConnect.rollback(con);
		} finally {
			DataConnect.close(con);
		}
		navigation.setId(barcode);
	}

	protected void setInfos(List<TaskInfo> infos) {
		if (infos == null || infos.size() == 0) {
			String[] o = { "infos", "info", "page", "result" };
			sessionHelper.remove(o);
			sessionHelper.removeContext("infos");
			sessionHelper.removeContext("info");
			sessionHelper.removeContext("page");
			sessionHelper.removeContext("result");

			return;
		}
		if (infos.size() > 0) {
			sessionHelper.putContext("infos", infos);
			sessionHelper.putContext("info", infos.get(0));
			sessionHelper.putContext("page", new Page(infos.size(), 1));
			sessionHelper.removeContext("result");
		} else if (infos.size() == 1) {
			sessionHelper.removeContext("page");
			sessionHelper.removeContext("infos");
			sessionHelper.removeContext("result");
		}
	}

	protected ResultInfo excute(Connection con, String id) throws Exception {
		return null;
	}

	public void excute() {
		String id = params.get("id");

		Connection con = null;
		try {
			con = DataConnect.getConnect();
			ResultInfo result = excute(con, id);

			sessionHelper.putContext("result", result);
			con.commit();
		} catch (Exception e) {
			DataConnect.rollback(con);
			logger.error(e.getMessage());
			ResultInfo	result = new ResultInfo("1");
			String msg = e.getMessage();
			int a = msg.indexOf("\n");
			if(a>0)
			 msg = msg.substring(0,a).trim();
			msg = msg.replace("\'", "");
			msg = msg.replace("\"", "");
			result.setMessage(msg);
			sessionHelper.putContext("result", result);
		} finally {
			DataConnect.close(con);
		}
	}

	public void show() {
		List<TaskInfo> infos = (ArrayList<TaskInfo>) sessionHelper
				.getContext("infos");
		String index = params.get("index");
		int row = Integer.parseInt(index) - 1;
		if (infos == null) {
			sessionHelper.remove();
			return;
		}
		if (row >= 0 && row <= infos.size()) {
			sessionHelper.putContext("info", infos.get(row));
			sessionHelper.putContext("page", new Page(infos.size(), row + 1));
		}
		sessionHelper.removeContext("result");
	}

	@SuppressWarnings("unchecked")
	protected List<TaskInfo> removeInfo(TaskInfo info) {
		List<TaskInfo> infos = (ArrayList<TaskInfo>) sessionHelper
				.getContext("infos");
		if (infos == null) {
			sessionHelper.removeContext("info");
			return null;
		}
		int size = infos.size();
		for (int j = 0; j < size; j++) {
			if (infos.get(j).equals(info)) {
				infos.remove(j);
				break;
			}
		}

		if (infos.size() == 0) {
			sessionHelper.removeContext("infos");
			sessionHelper.removeContext("info");
			sessionHelper.removeContext("page");
		} else {
			sessionHelper.putContext("info", infos.get(0));
			sessionHelper.putContext("page", new Page(infos.size(), 1));
		}
		return infos;
	}

}
